package service;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import util.MybatisUtil;

import dao.WorkingMapper;
import entity.Working;

public class WorkingService {

	/**
	 * 查询所有工作笔记信息
	 * @return
	 */
	public List<Working> getList(){
		SqlSession sqlSession = null;
		List<Working> wList = new ArrayList<Working>();
		try {
			sqlSession = MybatisUtil.createSqlSession();

			wList = sqlSession.getMapper(WorkingMapper.class).getList();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return wList;
	}

	/**
	 * 新增工作笔记信息
	 * @param w
	 * @return
	 */
	public int save(Working w){
		SqlSession sqlSession = null;
		int count = 0;
		try {
			sqlSession = MybatisUtil.createSqlSession();
			count = sqlSession.getMapper(WorkingMapper.class).save(w);

			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return count;
	}

	/**
	 * 根据编号查询工作笔记信息
	 * @return
	 */
	public Working getWorking(int id){
		SqlSession sqlSession = null;
		Working wList = new Working();
		try {
			sqlSession = MybatisUtil.createSqlSession();
			wList = sqlSession.getMapper(WorkingMapper.class).getWorking(id);

			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return wList;
	}

	public int getCount(String title){
		SqlSession sqlSession = null;
		int count = 0;
		try {
			sqlSession = MybatisUtil.createSqlSession();
			count = sqlSession.getMapper(WorkingMapper.class).getCount(title);

			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return count;
	}

	/**
	 * 获取总记录数
	 * @return
	 */
	public int getTotalCount(){
		SqlSession sqlSession = null;
		int count = 0;
		try {
			sqlSession = MybatisUtil.createSqlSession();
			count = sqlSession.getMapper(WorkingMapper.class).getTotalCount();

			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		}finally{
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return count;
	}

	/**
	 * 查询所有工作笔记信息
	 * @return
	 */
	public List<Working> getListPage(int beginIndex,int pageSize){
		SqlSession sqlSession = null;
		List<Working> wList = new ArrayList<Working>();
		try {
			sqlSession = MybatisUtil.createSqlSession();

			wList = sqlSession.getMapper(WorkingMapper.class).getListPage(beginIndex,pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			MybatisUtil.closeSqlSession(sqlSession);
		}
		return wList;
	}
	
}
